package com.zhijian.wharf.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.zhijian.util.JsonUtil;
import com.zhijian.wharf.entity.dto.WeatherDto;
import com.zhijian.wharf.entity.po.Weather;
import com.zhijian.wharf.service.WeatherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author HCG
 * @version 1.0
 * @date 2024-08-18
 */
@Service
@Slf4j
public class WeatherJob {

    @Autowired
    private WeatherService weatherService;

    /**
     * 一天执行一次
     */
    @Scheduled(cron = "${job.point.cron:0 6 1 * * * }")
    public void handle() {
        String url = String.format("https://restapi.amap.com/v3/weather/weatherInfo?key=%s&city=%s&extensions=%s&output=JSON",
                "8b675f803a92d0d452951a152cf90fa7", "330400", "all");
        //链式构建请求
        try {
            HttpResponse httpResponse = HttpRequest.get(url)
                    //超时，毫秒
                    .timeout(1000 * 20)
                    .execute();
            String responseBody = httpResponse.body();
//            log.info("select EnvMonitorDataDto response body is {}", responseBody);
            try {
                WeatherDto weatherDto = JsonUtil.toBean(responseBody, WeatherDto.class);
                if (Objects.nonNull(weatherDto)) {
                    Weather weather = new Weather();
                    weather.setCity("嘉兴");
                    List<WeatherDto.ForecastsBean.CastsBean> casts = weatherDto.getForecasts().get(0).getCasts();
                    String today = DateUtil.formatDate(new Date());
                    for (WeatherDto.ForecastsBean.CastsBean cast : casts) {
                        if (today.equals(cast.getDate())) {
                            weather.setDate(cast.getDate());
                            weather.setDayPower(cast.getDaypower());
                            weather.setNightPower(cast.getNightpower());
                            weather.setDayWeather(cast.getDayweather());
                            weather.setNightWeather(cast.getNightweather());
                            weather.setDayTemp(cast.getDaytemp());
                            weather.setNightTemp(cast.getNighttemp());
                            weather.setDayWind(cast.getDaywind());
                            weather.setNightWind(cast.getNightwind());
                            weatherService.insert(weather);
                        }
                    }
                }
            } catch (Exception e) {
                log.error("the response body of select weather is not legal...", e.getMessage());
                e.printStackTrace();
            }
        } catch (Exception e) {
            log.error("the request of select weather cause error...", e.getMessage());
            e.printStackTrace();
        }
    }

}
